﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Epsilon.Typing;
using Epsilon.Expr;

namespace Epsilon.Output
{
    public partial class Writer
    {
        public void WritePattern(IPattern p)
        {
            if (p is VarExpr)
                WritePattern((VarExpr)p);
            else if (p is PatternTuple)
                WritePattern((PatternTuple)p);
            else if (p is PatternRec)
                WritePattern((PatternRec)p);
            else
                throw new OutputException("Invalid pattern expression");
        }

        public void WritePattern(VarExpr p)
        {
            Output(p.Var);
        }

        public void WritePattern(PatternTuple p)
        {
            Output("{");
            for (int i = 0; i < p.Patterns.Count; i++)
            {
                if (i > 0)
                    Output(", ");
                WritePattern(p.Patterns[i]);
            }
            Output("}");
        }

        public void WritePattern(PatternRec p)
        {
            Output("#" + p.Name + "{");
            for (int i = 0; i < p.Fields.Count; i++)
            {
                if (i > 0)
                    Output(", ");
                Output(p.Fields[i].Name + "=");
                WritePattern(p.Fields[i].Pattern);
            }
            Output("}");
        }
    }
}
